home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 11: TSX-11 / Linux Cubed Series 11 - TSX-11 Vol 1.iso / sbin / bootutil.1 / bootutil / bootutils / rdev.c next >
Encoding:
C/C++ Source or Header  |  1993-07-01  |  6.4 KB  |  240 lines

  1. /*
  2.  
  3.   rdev.c  -  query/set root device.
  4.  
  5. -------------------------------------------------------------------------
  6.  
  7. Date: Sun, 27 Dec 1992 15:55:31 +0000
  8. Subject: Re: rdev
  9. From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
  10. To: Rik Faith <faith@cs.unc.edu>
  11.  
  12. There are quite a few versions of rdev:
  13.  
  14.   - the original rootdev that only printed the current root device, by
  15.     Linus.
  16.   - rdev that does what rootdev did and that also allows you to change
  17.     the root (and swap) device, by me.
  18.   - rdev got renamed to setroot and I think even to rootdev on various
  19.     distributions.
  20.   - Peter MacDonald added video mode and RAM disk setting and included
  21.     this version on SLS, called rdev again. I've attached his rdev.c to
  22.     this mail.
  23.     
  24. -------------------------------------------------------------------------
  25.     
  26. Date: 11 Mar 92 21:37:37 GMT
  27. Subject: rdev - query/set root device
  28. From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
  29. Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
  30.  
  31. With all that socket, X11, disk driver and FS hacking going on, apparently
  32. nobody has found time to address one of the minor nuisances of life: set-
  33. ting the root FS device is still somewhat cumbersome. I've written a little
  34. utility which can read and set the root device in boot images:
  35.  
  36. rdev accepts an optional offset argument, just in case the address should
  37. ever move from 508. If called without arguments, rdev outputs an mtab line
  38. for the current root FS, just like /etc/rootdev does.
  39.  
  40. ramsize sets the size of the ramdisk.  If size is zero, no ramdisk is used.
  41.  
  42. vidmode sets the default video mode at bootup time.  -1 uses default video
  43. mode, -2 uses menu.
  44.  
  45. -------------------------------------------------------------------------
  46.  
  47. Sun Dec 27 10:42:16 1992: Minor usage changes, faith@cs.unc.edu.
  48. Tue Mar 30 09:31:52 1993: rdev -Rn to set root readonly flag, sct@dcs.ed.ac.uk
  49.  
  50. -------------------------------------------------------------------------
  51.  
  52. */
  53.  
  54. /* rdev.c  -  query/set root device. */
  55.  
  56. usage()
  57. {
  58.  
  59.     puts("usage: rdev [ -rsv ] [ -o OFFSET ] [ IMAGE [ VALUE [ OFFSET ] ] ]");
  60.     puts("  rdev /dev/fd0  (or rdev /linux, etc.) displays the current ROOT device");
  61.     puts("  rdev /dev/fd0 /dev/hda2         sets ROOT to /dev/hda2");
  62.     puts("  rdev -R /dev/fd0 1              set the ROOTFLAGS (readonly status)");
  63.     puts("  rdev -s /dev/fd0 /dev/hda2      set the SWAP device");
  64.     puts("  rdev -r /dev/fd0 627            set the RAMDISK size");
  65.     puts("  rdev -v /dev/fd0 1              set the bootup VIDEOMODE");
  66.     puts("  rdev -o N ...                   use the byte offset N");
  67.     puts("  rootflags ...                   same as rdev -R");
  68.     puts("  swapdev ...                     same as rdev -s");
  69.     puts("  ramsize ...                     same as rdev -r");
  70.     puts("  vidmode ...                     same as rdev -v");
  71.     puts("Note: video modes are: -3=Ask, -2=Extended, -1=NormalVga, 1=key1, 2=key2,...");
  72.     puts("      use -R 1 to mount root readonly, -R 0 for read/write.");
  73.     exit(-1);
  74. }
  75.  
  76. #include <stdio.h>
  77. #include <string.h>
  78. #include <ctype.h>
  79. #include <errno.h>
  80. #include <fcntl.h>
  81. #include <dirent.h>
  82. #include <unistd.h>
  83. #include <stdlib.h>
  84. #include <sys/types.h>
  85. #include <sys/stat.h>
  86.  
  87.  
  88. #define DEFAULT_OFFSET 508
  89.  
  90.  
  91. static void die(char *msg)
  92. {
  93.     perror(msg);
  94.     exit(1);
  95. }
  96.  
  97.  
  98. static char *find_dev(int number)
  99. {
  100.     DIR *dp;
  101.     struct dirent *dir;
  102.     static char name[PATH_MAX+1];
  103.     struct stat s;
  104.  
  105.     if (!number) return "Boot device";
  106.     if ((dp = opendir("/dev")) == NULL) die("opendir /dev");
  107.     strcpy(name,"/dev/");
  108.     while (dir = readdir(dp)) {
  109.     strcpy(name+5,dir->d_name);
  110.     if (stat(name,&s) < 0) die(name);
  111.     if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number) return name;
  112.     }
  113.     sprintf(name,"0x%04x",number);
  114.     return name;
  115. }
  116.  
  117. /* enum { RDEV, SDEV, RAMSIZE, VIDMODE }; */
  118. enum { RDEV, VIDMODE, RAMSIZE, SDEV, __syssize__, ROOTFLAGS };
  119. char *cmdnames[6] = { "rdev", "vidmode",  "ramsize", "swapdev", 
  120.               "", "rootflags"};
  121. char *desc[6] = { "Root device", "Video mode",  "Ramsize",  "Swap device",
  122.           "", "Root flags"};
  123. #define shift(n) argv+=n,argc-=n
  124.  
  125. int main(int argc,char **argv)
  126. {
  127.     int image,offset,dev_nr, i, newoffset=-1;
  128.     char *device, cmd = 0, *ptr, tmp[40];
  129.     struct stat s;
  130.  
  131.     device = NULL;
  132.     if (ptr = strrchr(argv[0],'/'))
  133.         ptr++;
  134.     else
  135.         ptr = argv[0];
  136.     for (i=0; i<=5; i++)
  137.         if (!strcmp(ptr,cmdnames[i]))
  138.         break;
  139.     cmd = i;
  140.     if (cmd>5)
  141.         cmd=RDEV;
  142.     offset = DEFAULT_OFFSET-cmd*2;
  143.  
  144.     while (1)
  145.     { 
  146.         if (argv[1][0] != '-')
  147.            break;
  148.     else
  149.         switch (argv[1][1])
  150.         {
  151.         case 'R':
  152.             cmd=ROOTFLAGS;
  153.                 offset = DEFAULT_OFFSET-cmd*2;
  154.                 shift(1);
  155.             break;
  156.             case 'r': 
  157.                 cmd=RAMSIZE;
  158.                 offset = DEFAULT_OFFSET-cmd*2;
  159.                 shift(1);
  160.             break;
  161.         case 'v':
  162.                 cmd=VIDMODE;
  163.                 offset = DEFAULT_OFFSET-cmd*2;
  164.                 shift(1);
  165.             break;
  166.         case 's':
  167.                 cmd=SDEV;
  168.                 offset = DEFAULT_OFFSET-cmd*2;
  169.                 shift(1);
  170.             break;
  171.         case 'o':
  172.             if (argv[1][2])
  173.             {
  174.                 newoffset=atoi(argv[1]+2);
  175.                 shift(1);
  176.             } else {
  177.                 newoffset=atoi(argv[2]);
  178.                 shift(2);
  179.             }
  180.             break;
  181.         default:
  182.             usage();
  183.          }
  184.     }
  185.     if (newoffset >= 0)
  186.     offset = newoffset;
  187.  
  188.     if  ((cmd==RDEV) && (argc == 1 || argc > 4)) {
  189.     if (stat("/",&s) < 0) die("/");
  190.     printf("%s /\n",find_dev(s.st_dev));
  191.     exit(0);
  192.     } else if ((cmd != RDEV) && (argc == 1 || argc > 4)) usage();
  193.  
  194.     if ((cmd==RDEV) || (cmd==SDEV))
  195.     {    
  196.         if (argc == 4) {
  197.         device = argv[2];
  198.         offset = atoi(argv[3]);
  199.         }
  200.         else {
  201.         if (argc == 3) {
  202.             if (isdigit(*argv[2])) offset = atoi(argv[2]);
  203.             else device = argv[2];
  204.         }
  205.         }
  206.     }
  207.     else
  208.     {
  209.         if (argc>=3)
  210.         device = argv[2];
  211.         if (argc==4)
  212.         offset = atoi(argv[3]);
  213.     }
  214.     if (device) {
  215.         if ((cmd==SDEV) || (cmd==RDEV))
  216.     {    if (stat(device,&s) < 0) die(device);
  217.     } else
  218.         s.st_rdev=atoi(device);
  219.     if ((image = open(argv[1],O_WRONLY)) < 0) die(argv[1]);
  220.     if (lseek(image,offset,0) < 0) die("lseek");
  221.     if (write(image,(char *)&s.st_rdev,2) != 2) die(argv[1]);
  222.     if (close(image) < 0) die("close");
  223.     }
  224.     else {
  225.     if ((image = open(argv[1],O_RDONLY)) < 0) die(argv[1]);
  226.     if (lseek(image,offset,0) < 0) die("lseek");
  227.     dev_nr = 0;
  228.     if (read(image,(char *)&dev_nr,2) != 2) die(argv[1]);
  229.     if (close(image) < 0) die("close");
  230.     printf(desc[cmd]);
  231.     if ((cmd==SDEV) || (cmd==RDEV))
  232.         printf(" %s\n", find_dev(dev_nr));
  233.     else
  234.         printf(" %d\n", dev_nr);
  235.     }
  236.     return 0;
  237. }
  238.  
  239.  
  240.